//
//  ViewBuilderShortcut.swift
//  EasyCoding
//
//  Created by Fanxx on 2019/12/20.
//

import UIKit

/// easy.build里面的每一个项目
public protocol ECViewBuildable {
    /// 添加自身到交视图
    func __easyPerformBuild(add: (UIView) -> Void)
    /// 执行布局
    func __easyPerformBuildLayout()
}

// MARK:  空处理
public struct ECViewBuildItemNone: ECViewBuildable {
    public func __easyPerformBuild(add: (UIView) -> Void) { }
    public func __easyPerformBuildLayout() { }
}

// MARK:  数组处理
public struct ECViewBuildItemArray: ECViewBuildable {
    var items: [ECViewBuildable]
    
    public func __easyPerformBuild(add: (UIView) -> Void) {
        self.items.forEach({ $0.__easyPerformBuild(add: add)})
    }
    public func __easyPerformBuildLayout() {
        self.items.forEach({ $0.__easyPerformBuildLayout()})
    }
}

// MARK:  单视图处理

/// 默认可传UIView
extension UIView: ECViewBuildable {
    public func __easyPerformBuild(add: (UIView) -> Void) {
        add(self)
    }
    public func __easyPerformBuildLayout() {
        
    }
}
/// 默认可传UIView().easy
extension ECNamespaceWrapper: ECViewBuildable where Base: UIView {
    public func __easyPerformBuild(add: (UIView) -> Void) {
        add(self.base)
    }
    public func __easyPerformBuildLayout() {
        
    }
}
/// 默认可传UIView().easy.layout()
extension ECViewLayoutBuffer: ECViewBuildable {
    public func __easyPerformBuild(add: (UIView) -> Void) {
        add(self.view)
    }
    public func __easyPerformBuildLayout() {
        self.apply()
    }
}

// MARK:  多视图(数组)处理

/// 默认可传[UIView]
extension Array: ECViewBuildable where Element: UIView {
    public func __easyPerformBuild(add: (UIView) -> Void) {
        self.forEach(add)
    }
    public func __easyPerformBuildLayout() {
        
    }
}
/// 默认可传[UIView].easy
extension ECNamespaceArrayWrapper: ECViewBuildable where Element: UIView {
    public func __easyPerformBuild(add: (UIView) -> Void) {
        self.base.forEach(add)
    }
    public func __easyPerformBuildLayout() {
        
    }
}
/// 默认可传[UIView()].easy.layout()
extension ECViewLayoutArrayBuffer: ECViewBuildable {
    public func __easyPerformBuild(add: (UIView) -> Void) {
        self.views.forEach(add)
    }
    public func __easyPerformBuildLayout() {
        self.apply()
    }
}
